package com.demo.modules.mq.rabbit.service.dead;

import com.demo.modules.mq.rabbit.RabbitAnnouncementMessage;
import com.demo.modules.mq.rabbit.configuration.DeadConfiguration;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * 死信队列消息消费者
 *
 * @author Rog
 */
@Component
@Slf4j
public class RabbitDeadMessageConsumerService {

    /**
     * 监听死信队列
     */
    @RabbitListener(queues = DeadConfiguration.DEAD_QUEUE)
    public void listenDeadQueue(RabbitAnnouncementMessage rabbitAnnouncementMessage, Channel channel) {
        log.info("接收到死信队列消息:{}", rabbitAnnouncementMessage);
    }

    /**
     * 监听普通队列
     */
    @RabbitListener(queues = "formal.queue11")
    public void listenFormalQueue(Message message, RabbitAnnouncementMessage rabbitAnnouncementMessage, Channel channel) throws IOException {
        log.info("接收到普通队列消息:{}", rabbitAnnouncementMessage);
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //拒绝消息
        channel.basicReject(deliveryTag, false);
    }
}
